home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / CSharp / Applications / Process View / ProcessClasses.cs < prev    next >
Encoding:
Text File  |  2004-10-22  |  3.3 KB  |  126 lines

  1. /*******************************************************************************
  2.   ProcessChecker Demo
  3.   Example submitted by  by David Clegg
  4.  
  5.   This unit contains classes used to hold details for monitored and dependant
  6.   processes.
  7. *******************************************************************************/
  8. using System;
  9. using System.Collections;
  10. using System.Runtime.Serialization;
  11. using System.IO;
  12.  
  13. namespace ProcessChecker
  14. {
  15.     /// <summary>
  16.     /// Base class to contain details of processes to monitor
  17.     /// </summary>
  18.     [Serializable]
  19.     public class ProcessDetails {
  20.         private string fProcessPath;
  21.  
  22.         /// <summary>
  23.         /// The full path, including the executable name, for the process.
  24.         /// </summary>
  25.         public string ProcessPath {
  26.             get {return fProcessPath;}
  27.             set {fProcessPath = value;}
  28.         }
  29.  
  30.         public ProcessDetails(string processPath) {
  31.           fProcessPath = processPath;
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Name of the process to be used for calls to Process.GetProcesses.
  36.         /// </summary>
  37.         public string ProcessName {
  38.             get {return Path.GetFileNameWithoutExtension(fProcessPath);}
  39.         }
  40.     }
  41.  
  42.     /// <summary>
  43.     /// Class to represent a process that is to be watched, and restarted if
  44.     /// necessary.
  45.     /// </summary>
  46.     [Serializable]
  47.     public class WatchedProcess : ProcessDetails {
  48.  
  49.         private ProcessList fDependantProcesses;
  50.  
  51.         private bool fCheckResponding = true;
  52.  
  53.         public WatchedProcess(string processName) : base(processName) {
  54.             fDependantProcesses = new ProcessList();
  55.         }
  56.  
  57.         /// <summary>
  58.         /// List of all dependant processes associated with this process.
  59.         /// Dependant processes will be terminated before the watched process
  60.         /// is restarted.
  61.         /// </summary>
  62.         public ProcessList DependantProcesses {
  63.             get {return fDependantProcesses;}
  64.         }
  65.  
  66.         /// <summary>
  67.         /// Indicates whether ProcessChecker should check if the process is
  68.         /// responding, and restart it if it isn't.
  69.         /// </summary>
  70.         public bool CheckResponding {
  71.             get {return fCheckResponding;}
  72.             set {fCheckResponding = value;}
  73.         }
  74.     }
  75.  
  76.     /// <summary>
  77.     /// Class to represent a process that is a dependant of a watched process.
  78.     /// If the watched process needs to be restarted, its dependants must be
  79.     /// terminated first.
  80.     /// </summary>
  81.     [Serializable]
  82.     public class DependantProcess : ProcessDetails {
  83.         [NonSerialized]
  84.         ProcessDetails fParent;
  85.         public DependantProcess(string processName, ProcessDetails parent) : base(processName) {
  86.             fParent = parent;
  87.         }
  88.  
  89.         public ProcessDetails Parent {
  90.             get {return fParent;}
  91.         }
  92.     }
  93.  
  94.     /// <summary>
  95.     /// Strongly typed container class to hold references to multiple ProcessDetails
  96.     /// classes.
  97.     /// </summary>
  98.     [Serializable]
  99.     public class ProcessList : CollectionBase {
  100.         public ProcessDetails this[int index] {
  101.             get {return (ProcessDetails)List[index];}
  102.             set {List[index] = value;}
  103.         }
  104.  
  105.         public int Add(ProcessDetails process) {
  106.             return List.Add(process);
  107.         }
  108.  
  109.         public void Insert(int index, ProcessDetails process) {
  110.             List.Insert(index, process);
  111.         }
  112.  
  113.         public void Remove(ProcessDetails process) {
  114.             List.Remove(process);
  115.         }
  116.  
  117.         public bool Contains(ProcessDetails process) {
  118.             return List.Contains(process);
  119.         }
  120.  
  121.         public int IndexOf(ProcessDetails process) {
  122.             return List.IndexOf(process);
  123.         }
  124.     }
  125. }
  126.